package com.example.mybatislearning.registrar;

import com.example.mybatislearning.again.MyClassAnother;
import com.example.mybatislearning.again.MyFactoryBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @author zhangfei
 * @date 2024/3/13 17:35
 **/
@Component
public class MyClassRegistrarAnother implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MyClassAnother.class);
        AbstractBeanDefinition definition = builder.getBeanDefinition();
        String beanClassName = definition.getBeanClassName();
        definition.setBeanClass(MyFactoryBean.class);
        definition.getConstructorArgumentValues().addGenericArgumentValue(beanClassName);
        definition.getPropertyValues().add("sqlSessionTemplate",
                new RuntimeBeanReference("sqlSessionTemplate"));
        registry.registerBeanDefinition(beanClassName,definition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
